package com.ece.ecofitness.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ece.ecofitness.jdbcConnection.JdbcConnection;
import com.ece.ecofitness.model.Article;
import com.ece.ecofitness.model.Commande;
import com.ece.ecofitness.model.User;

@Service
public class CommandeDaoImp implements CommandeDao{

	@Autowired
	private JdbcConnection jdbcConnection;

	public List<Commande> getAllCommande() {
		try {
			ArrayList<Commande> array=new ArrayList<Commande>();
			Article article=null;
			User user=null;
			Commande commande=null;SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ResultSet resultat = jdbcConnection.getStat().executeQuery("SELECT login,nom,prenom,idArticle,label,description,art.point,idCommande,dateCommande,dateLivraison,dateReception,art.photo FROM commande as cmd inner join Article as art on art.idArticle=cmd.article inner join user on user.login = cmd.client");
			Date dateCommande,dateLivraison,dateReception;
			while(resultat.next()){
				
				user=new User(resultat.getString(1),null,false,resultat.getString(2),resultat.getString(3));
				
				article=new Article(Integer.parseInt(resultat.getString(4)), resultat.getString(5), resultat.getString(6), Integer.parseInt(resultat.getString(7)),resultat.getString(12));
				
				if(resultat.getString(9)!=null)
					dateCommande=dateFormat.parse(resultat.getString(9));
				else 
					dateCommande=null;
				
				if(resultat.getString(10)!=null)
					dateLivraison=dateFormat.parse(resultat.getString(10));
				else 
					dateLivraison=null;
				
				if(resultat.getString(11)!=null)
					dateReception=dateFormat.parse(resultat.getString(11));
				else 
					dateReception=null;
				
				commande=new Commande(Integer.parseInt(resultat.getString(8)),user, article, dateCommande, dateLivraison,dateReception);
				
				array.add(commande);
			}	
			
			return array;
		} catch (SQLException e) {
			e.printStackTrace();return null;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return null;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Commande updateCommande(int idCommande, Date dateLivraison,
			Date dateReception) {
		try {
			String cmd="";
			
			if (dateLivraison!=null)
				cmd+="set dateLivraison='"+new Timestamp(dateLivraison.getTime())+"'";
			
			if (dateReception!=null)
			{
				if(cmd.equals(""))
					cmd+="set dateReception='"+new Timestamp(dateReception.getTime())+"'";
				else
					cmd+=", dateReception='"+new Timestamp(dateReception.getTime())+"'";
			}
			
			if(!cmd.equals(""))
			{
				if(jdbcConnection.getStat().executeUpdate("UPDATE commande "+cmd+" WHERE idCommande ='"+idCommande+"' ")>0)
				{
					jdbcConnection.getConnection().commit();
					return getCommande(idCommande);
				}
				else return null;
			}
			else return null;
		}
		catch (SQLException e1) {
			return null;
		}
	}

	public Commande getCommande(int idCommande) {
		try {
			Article article=null;
			User user=null;
			Commande commande=null;
			Date dateCommande=null;
			Date dateLivraison =null;
			Date dateReception=null;
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ResultSet resultat = jdbcConnection.getStat().executeQuery("SELECT login,nom,prenom,idArticle,label,description,art.point,idCommande,dateCommande,dateLivraison,dateReception,art.photo FROM commande as cmd inner join Article as art on art.idArticle=cmd.article inner join user on user.login = cmd.client where cmd.idCommande='"+idCommande+"'");
			if(resultat.next()){
				
				user=new User(resultat.getString(1),null,false,resultat.getString(2),resultat.getString(3));
				
				article=new Article(Integer.parseInt(resultat.getString(4)), resultat.getString(5), resultat.getString(6), Integer.parseInt(resultat.getString(7)),resultat.getString(12));
				
				if(resultat.getString(9)!=null)
					dateCommande=dateFormat.parse(resultat.getString(9));
				
				if(resultat.getString(10)!=null)
					dateLivraison=dateFormat.parse(resultat.getString(10));
				
				if(resultat.getString(11)!=null)
					dateReception=dateFormat.parse(resultat.getString(11));
				
				commande=new Commande(Integer.parseInt(resultat.getString(8)),user, article, dateCommande, dateLivraison,dateReception);
			}	
			
			return commande;
		} catch (SQLException e) {
			e.printStackTrace();return null;
		} catch (NumberFormatException e) {
			e.printStackTrace();return null;
		} catch (ParseException e) {
			e.printStackTrace();return null;
		}

	}
	
}
